package com.wson.render.clouddisk;

import java.util.Map;

import com.wson.behavior.ctrl.IBehavior;
import com.wson.behavior.layout.ITable;
import com.wson.constant.IRuntimeConstant;
import com.wson.controller.constant.IControllerConstant;
import com.wson.controller.oauth.AbstractOAuthProvidersCenterRender;
import com.wson.model.adapter.oauthaccount.IOAuthAccountView;
import com.wson.model.adapter.oauthprovider.IOAuthProviderMeta;
import com.wson.model.adapter.oauthprovider.IOAuthProviderView;
import com.wson.model.basic.IBasicView;
import com.wson.model.clouddisk.provider.ICodiskProviderMeta;
import com.wson.model.clouddisk.provider.impl.CodiskProviderModel;
import com.wson.model.framework.application.IApplicationMeta;
import com.wson.platform.factory.impl.BeanFactory;
import com.wson.root.util.ExceptionUtil;
import com.wson.util.function.HttpUtil;

public abstract class AbstractCodiskProvidersCenterRender extends
		AbstractOAuthProvidersCenterRender {

	@Override
	protected IOAuthProviderView generateProvider() {
		return this.initModel((IOAuthProviderView) new CodiskProviderModel());
	}


	@Override
	public IBehavior render() {
		this.setModelMetaID(ICodiskProviderMeta.META_ID);
		this.setEditable(false);
		this.setTableType(ITable.APPEARANCETYPE_BLOCKTABLE);
		this.setShowShortText(true);
		this.setFramed(false);
		ITable behavior = (ITable) super.render();
		behavior.getBlock().setColumnsNumber(6);
		// System.out.println("behavior "+behavior.generateBehaviorCode());
		return behavior;
	}
	
	protected abstract String getActionOAuthCertificationRequest();
	protected abstract String getActionOAuthActivation();
	@Override
	protected String generateIconNavigationHref(Map<String, Object> structure) {
		return generateCertificationRequestHref(
				this.getActionOAuthCertificationRequest(),
				structure, this.getActionOAuthActivation(),
				ICodiskProviderMeta.SYNC_SELF);
	}

	protected String generateCertificationRequestHref(String requestaction,
			Map<String, Object> structure, String callbackaction,
			String sync_provider) {

		String callback = this.generateCallbackHref(callbackaction,
				sync_provider, structure);
		
		//System.out.println("callback "+callback);

		String result = requestaction
				+ "?"
				+ HttpUtil.generateHttpParam(
						IControllerConstant.REQUEST_ACTION,
						IOAuthAccountView.ACTION_CERTIFICATIONREQUEST)
				+ HttpUtil
						.generateHttpParam(IOAuthProviderMeta.SYNC_ID,
								((IBasicView) structure
										.get(IBasicView.BEHAVIORFEATURE_MODEL))
										.getID())
				+ HttpUtil.generateHttpParam(IOAuthProviderMeta.SYNC_METAID,
						structure.get(IBasicView.BEHAVIORFEATURE_META))
		// + HttpUtil.generateHttpParam(IModuleMeta.SYNC_SELF,
		// this.getModuleID())
		;
		try {
			result += HttpUtil.generateHttpParam(
					IRuntimeConstant.CALLBACKFUNCTION,
					HttpUtil.encodeURLParam(callback)
					//URLEncoder.encode(callback, "utf-8")
					);
		} catch (Exception ex) {
			ExceptionUtil.handleException(ex);
		}
		return result;
	}

	private String generateCallbackHref(String callbackaction,
			String sync_provider, Map<String, Object> structure) {
		String callback = 
				BeanFactory.getCurrentFactory().getSystemProfile().getApplicationHost()
				+ callbackaction
				+ "?"
				+ HttpUtil.generateHttpParam(
						sync_provider,
						""
								+ ((IBasicView) structure
										.get(IBasicView.BEHAVIORFEATURE_MODEL))
										.getID())
				+ HttpUtil.generateHttpParam(IApplicationMeta.SYNC_SELF,
						BeanFactory.getCurrentFactory().getAppStub()
								.getApplicationID())
				// + HttpUtil.generateHttpParam(IModuleMeta.SYNC_SELF,
				// this.getModuleID())
				+ HttpUtil.generateHttpParam(IOAuthProviderMeta.SYNC_METAID,
						structure.get(IBasicView.BEHAVIORFEATURE_META));
		return callback;
	}

}
